package com.homework.papertexting;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean; // 导入 Bean 注解
import org.springframework.web.client.RestTemplate; // 导入 RestTemplate

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@SpringBootApplication
public class PapertextingApplication {

    @Value("${app.upload.dir:uploads/}")
    private String uploadDir;

    public static void main(String[] args) {
        SpringApplication.run(PapertextingApplication.class, args);
    }

    // 定义一个 Bean，在应用启动后运行，用于创建上传目录
    @Bean
    public CommandLineRunner createUploadDir() {
        return args -> {
            try {
                Path uploadPath = Paths.get(uploadDir);
                // 检查目录是否存在，如果不存在则创建
                if (!Files.exists(uploadPath)) {
                    Files.createDirectories(uploadPath);
                    System.out.println("上传目录创建成功: " + uploadPath.toAbsolutePath());
                } else {
                    System.out.println("上传目录已存在: " + uploadPath.toAbsolutePath());
                }
            } catch (Exception e) {
                // 如果创建目录失败，打印错误信息
                System.err.println("创建上传目录失败: " + e.getMessage());
                e.printStackTrace();
            }
        };
    }

    @Bean // 定义一个 RestTemplate Bean，供其他组件自动装配
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}